package com.sample.controller.datastore.ex2;


import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.sample.util.CustomQuery;

public class SearchController extends Controller {

    @Override
    public Navigation run() throws Exception {

        String price = asString("price");
        int priceFilter = asInteger("priceFilter");
        String sortDirection = asString("sortDirection");

        CustomQuery query = new CustomQuery("Goods");

        if (!"".equals(price)) {
            long priceLong = Long.parseLong(price);
            switch (priceFilter) {
                case 1:
                    query.eq("price", priceLong);
                    break;
                case 2:
                    query.ne("price", priceLong);
                    break;
                case 3:
                    query.gt("price", priceLong);
                    break;
                case 4:
                    query.ge("price", priceLong);
                    break;
                case 5:
                    query.lt("price", priceLong);
                    break;
                case 6:
                    query.le("price", priceLong);
                    break;
                default:
                    break;
            }
        }

        if ("asc".equals(sortDirection)) query.asc("price");
        if ("desc".equals(sortDirection)) query.desc("price");
        if ("asc".equals(sortDirection)) query.asc("cd");
        if ("desc".equals(sortDirection)) query.desc("cd");

        requestScope("result", query.asList().iterator());

        return forward("index.jsp");
    }
}
